import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/wroks/search_bean.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/ywx_limit.dart';

import '../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../manager/ywx_manager.dart';
import 'public/review_list.dart';

///工单中心
class TicketCenterRoute extends StatefulWidget {
  YwxItem item;

  TicketCenterRoute(this.item);

  @override
  _State createState() => _State();
}

class _State extends State<TicketCenterRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    if (presenter.searchInfo == null) {
      presenter.searchInfo = SearchInfo(widget.item);
      presenter.searchInfo?.factory = AppDataConfig.getFactory(); //获取当前默认工厂
    }
    initSearchInfo();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    LibRouteHelper.setResult(null);
  }

  ///是否切换了菜单
  bool isSwitchMenu = false;

  initSearchInfo() {
    isSwitchMenu = true;
    if (presenter.searchInfo != null) {
      presenter.searchInfo!..isShowStatus = false; //审核状态
      presenter.searchInfo!.isShowUserPerson = true; //报修人员
      presenter.searchInfo!.isEnableInputUserPerson = false; //报修人员,禁止手输。防止重名。
      presenter.searchInfo!.isShowAddTime = true; //日期
      presenter.searchInfo!.isShowDeviceCoding =
          false; //设备编码,不让用户手动输入，只允许扫描二维码。
      presenter.searchInfo!.isShowDeviceCodingWithUi=true;//搜索条件不显示，但是查询条件界面会显示。
    }
  }

  @override
  Widget build(BuildContext context) {
    // print("=====================================================================列表页面刷新");
    px.init(context);
    presenter.searchInfo?.showAddTime =
        AppStrings.of(context).Repairtime; //showAddTime 开始截止日期；Repairtime报修时间
    presenter.searchInfo?.showUserPerson =
        AppStrings.of(context).repairman; //保修人员
    if (presenter.searchInfo?.searchDatas == null ||
        presenter.searchInfo!.searchDatas!.length <= 0) {
      List<SearchBean>? searchDatas = [];
      //工单号
      searchDatas.add(SearchBean()
        ..showName = AppStrings.of(context).TicketNo
        ..id = "0");
      //等级
      var UrgencyLevelDatas = EquipmentInfo.getUrgencyLevels(context);
      searchDatas.add(SearchBean()
        ..showName = AppStrings.of(context).Lv
        ..id = "1"
        ..datas = UrgencyLevelDatas);
      //设备名称
      searchDatas.add(SearchBean()
        ..showName = AppStrings.of(context).Devicename
        ..id = "2");
      //工单类型
      var RepairTypeDatas = EquipmentInfo.getRepairTypeDatas(context);
      searchDatas.add(SearchBean()
        ..showName = AppStrings.of(context).Workordertype
        ..id = "3"
        ..datas = RepairTypeDatas);
      // //报修人员
      // searchDatas.add(SearchBean()
      //   ..showName = AppStrings.of(context).repairman
      //   ..id = "4");
      presenter.searchInfo?.searchDatas = searchDatas;
    }
    var scaffold = AppUi.getScaffoldWithReView(
        context,
        _body(context),
        widget.item,
        presenter.searchInfo!,
        () {
          //print("===============================================================搜索刷新");
          presenter.setStateBody?.call(() {});
        },
        scan: true,
        scanName: AppStrings.of(context).scanDeviceCode,
        scanCallBack: (valve) {
          //fixme 二维码扫描返回。
          presenter.setStateBody?.call(() {
            presenter.searchInfo!.DeviceCoding = valve; //设备编码
            presenter.searchInfo?.isScanDeviceCodingResult = true; //二维码扫描结果。
          });
        });
    return WillPopScope(
      child: scaffold,
      onWillPop: () async {
        return true; //true 不屏蔽返回键
      },
    );
  }

  Widget _body(BuildContext context) {
    //print("===============================================================页面刷新:$isShowPopuMenuWithSearch");
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      List<Widget> children = [];
      //搜索条件
      var searchLayout =
          AppUi.getSearchLayout(context, presenter.searchInfo!, () {
        presenter.setStateBody?.call(() {});
      }, ywxItem: widget.item, ywxItemCallback: () {});
      children.add(searchLayout);
      //审核列表
      var reviewList =
          ReviewList2(widget.item, presenter.searchInfo!, true, isSwitchMenu);
      if (isSwitchMenu) {
        isSwitchMenu = false;
      }
      children.add(Expanded(child: reviewList));
      return Column(
        children: children,
      );
    });
  }
}

class _Presenter {
  ///搜索条件
  SearchInfo? searchInfo = null;

  ///body视图状态
  StateSetter? setStateBody;
}
